﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Net;

namespace _4SquareLite
{
    public partial class frmDashboard : Form
    {
        string _strLoggedInUserDetailXML;
        string _strUserID;

        public frmDashboard(string strDefaultXML)
        {
            InitializeComponent();
            Program.arrForm.Add(this);
            _strLoggedInUserDetailXML = strDefaultXML;
        }

        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            if (inputPanel1.Enabled)
            {
                pnlForm.Height -= inputPanel1.Bounds.Height;
            }
            else
            {
                pnlForm.Height = this.Height;
            }
        }

        private void frmDashboard_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            string strXML = _strLoggedInUserDetailXML;

            // read the xml
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(strXML);
            XmlNode xmlRoot = xmlDocument.DocumentElement;

            // userid
            _strUserID = Program.getInnerText(xmlRoot, "id");

            // settings block
            XmlNode xmlSettings = xmlRoot.SelectSingleNode("settings");
            if (xmlSettings != null)
            {
                string strSetting = "";
                
                strSetting = Program.getInnerText(xmlSettings, "sendtotwitter");
                if (strSetting.Equals("true"))
                {
                    Program.bSendToTwitter = true;
                }

                strSetting = Program.getInnerText(xmlSettings, "sendtofacebook");
                if (strSetting.Equals("true"))
                {
                    Program.bSendToFacebook = true;
                }
            }

            // get the no of pending friend requests            
            string strFriendRequests = Program.getInnerText(xmlRoot, "status/friendrequests");
            if (String.IsNullOrEmpty(strFriendRequests))
            {
                btnPending.Enabled = false;
            }
            else
            {
                int i = int.Parse(strFriendRequests);

                if (i == 0)
                {
                    btnPending.Enabled = false;
                }
                else if (i == 1)
                {
                    btnPending.Text = "1 Pending Request";
                }
                else
                {
                    btnPending.Text = strFriendRequests + " Pending Requests";
                }
            }

            // lblName
            string strName = Program.getInnerText(xmlRoot, "firstname") + " " + Program.getInnerText(xmlRoot, "lastname");
            lblName.Text = strName.Trim();

            //gender
            string strGender = Program.getInnerText(xmlRoot, "gender");
            Color colorGender = Color.Black;
            switch (strGender)
            {
                case "female":
                    colorGender = Color.PaleVioletRed;
                    break;

                case "male":
                    colorGender = Color.SteelBlue;
                    break;
            }
            pnlGender.BackColor = colorGender;

            // pbUser
            string strURL = Program.getInnerText(xmlRoot, "photo");
            pbUser.Image = Program.getImageFromURL(strURL);
            
            //////////////////////////////////////////////////////////////////////////////
            // Get Venue History
            string strHistory = Program.HttpGet("http://api.foursquare.com/v1/history");

            if (!String.IsNullOrEmpty(strHistory))
            {
                Program.strGlobalLat = "";
                Program.strGlobalLon = "";

                XmlDocument xmlDocHistory = new XmlDocument();
                xmlDocHistory.LoadXml(strHistory);
                XmlNode xmlHistory = xmlDocHistory.DocumentElement;

                pnlHistory.Controls.Clear();
                int iHistoryY = 0;
                XmlNodeList xmlCheckins = xmlHistory.SelectNodes("checkin");
                foreach (XmlNode xmlCheckin in xmlCheckins)
                {
                    string strCreated = Program.getInnerText(xmlCheckin, "created");
                    strCreated = Program.convertAPITimeToCuteTime(strCreated);
                    XmlNode xmlVenue = xmlCheckin.SelectSingleNode("venue");
                    if (xmlVenue != null)
                    {
                        if ((String.IsNullOrEmpty(Program.strGlobalLat)) || (String.IsNullOrEmpty(Program.strGlobalLon)))
                        {
                            Program.strGlobalLat = Program.getInnerText(xmlVenue, "geolat");
                            Program.strGlobalLon = Program.getInnerText(xmlVenue, "geolong");
                        }

                        ucVenueListItem oVenue = new ucVenueListItem(xmlVenue, strCreated);
                        oVenue.Location = new Point(0, iHistoryY);
                        oVenue.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                        oVenue.Width = pnlHistory.Width;

                        pnlHistory.Controls.Add(oVenue);
                        pnlHistory.Height = oVenue.Location.Y + oVenue.Height;

                        iHistoryY += oVenue.Height + 3;

                        Panel oLine = new Panel();
                        oLine.Location = new Point(0, iHistoryY);
                        oLine.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                        oLine.Width = pnlHistory.Width;
                        oLine.Height = 1;
                        oLine.BackColor = Color.Black;

                        pnlHistory.Controls.Add(oLine);
                        pnlHistory.Height = oLine.Location.Y + oLine.Height;

                        iHistoryY += oLine.Height + 3;
                    }
                }
            }

            Cursor.Current = Cursors.Default;
        }

        private void btnPending_Click(object sender, EventArgs e)
        {
            frmUserList oForm = new frmUserList("Pending Friend Requests", "friendrequest");
            oForm.Show();
            this.Hide();
        }

        private void btnCheckin_Click(object sender, EventArgs e)
        {
            frmSearchVenue oForm = new frmSearchVenue();
            oForm.Show();
            this.Hide();
        }

        private void miRefresh_Click(object sender, EventArgs e)
        {
            string strURL = "http://api.foursquare.com/v1/user";
            
            Cursor.Current = Cursors.WaitCursor;
            _strLoggedInUserDetailXML = Program.HttpGet(strURL);
            Cursor.Current = Cursors.Default;

            if (!String.IsNullOrEmpty(_strLoggedInUserDetailXML))
            {
                frmDashboard_Load(sender, e);
            }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void miSearchUsers_Click(object sender, EventArgs e)
        {
            frmSearchUser oForm = new frmSearchUser();
            oForm.Show();
            this.Hide();
        }

        private void miLogout_Click(object sender, EventArgs e)
        {
            LocalSettings.Save("Authentication", "");

            int i = Program.arrForm.Count;
            if (i > 2) // if this is the 2nd form (eg: the 1st form is login)
            {
                i--;
                Program.arrForm.RemoveAt(i);
                i--;
                ((Form)Program.arrForm[i]).Show();
                this.Close();
            }
            else
            {
                frmLogin oForm = new frmLogin();
                oForm.Show();
                this.Hide();
            }
        }

        private void miCheckins_Click(object sender, EventArgs e)
        {
            frmCheckinList oForm = new frmCheckinList(Program.strGlobalLat, Program.strGlobalLon, "Friends' Check-Ins");
            oForm.Show();
            this.Hide();
        }

        private void miSearchVenues_Click(object sender, EventArgs e)
        {
            frmSearchVenue oForm = new frmSearchVenue();
            oForm.Show();
            this.Hide();
        }

        private void btnShout_Click(object sender, EventArgs e)
        {
            frmCheckin oForm = new frmCheckin();
            oForm.Show();
            this.Hide();
        }

        private void pbUser_Click(object sender, EventArgs e)
        {
            frmUserDetails oForm = new frmUserDetails(_strUserID);
            oForm.Show();
            this.Hide();
        }

        private void btnGPS_Click(object sender, EventArgs e)
        {
            frmGPS oForm = new frmGPS();
            oForm.Show();
            this.Hide();
        }
    }
}